package com.jiutong.meipenggang.ui.activitys;


import android.text.Html;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.BaseBean;
import com.jiutong.meipenggang.http.model.interfaces.GestureBiz;
import com.jiutong.meipenggang.http.present.GesturePresent;
import com.jiutong.meipenggang.utils.EncryptUtil;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.views.gesturelock.widget.GestureContentView;
import com.jiutong.meipenggang.views.gesturelock.widget.GestureDrawline;
import com.jiutong.meipenggang.views.gesturelock.widget.LockIndicator;
import com.orhanobut.logger.Logger;

public class GestureEditActivity extends BaseActivity implements View.OnClickListener, GestureBiz {
    /**
     * 手机号码
     */
    public static final String PARAM_PHONE_NUMBER = "PARAM_PHONE_NUMBER";
    /**
     * 意图
     */
    public static final String PARAM_INTENT_CODE = "PARAM_INTENT_CODE";
    /**
     * 首次提示绘制手势密码，可以选择跳过
     */
    public static final String PARAM_IS_FIRST_ADVICE = "PARAM_IS_FIRST_ADVICE";
    private TextView mTextTitle;
    private TextView mTextCancel;
    private LockIndicator mLockIndicator;
    private TextView mTextTip;
    private FrameLayout mGestureContainer;
    private GestureContentView mGestureContentView;
    private TextView mTextReset;
    private String mParamSetUpcode = null;
    private String mParamPhoneNumber;
    private boolean mIsFirstInput = true;
    private String mFirstPassword = null;
    private String mConfirmPassword = null;
    private int mParamIntentCode;

    //    private SharedPreferences mSharedPreferences = null;
    private String shoushi;
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_gesture_edit);
//        setUpViews();
//        setUpListeners();
//    }

    private GesturePresent gesturePresent;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_gesture_edit;

    }

    @Override
    protected void initView() {
        super.initView();
        gesturePresent = new GesturePresent(this, this);
        shoushi = SpConfig.getInstance().getString(Constants.SHOU_SHI);
        setUpViews();
        setUpListeners();
    }

    private void setUpViews() {
        mTextTitle = (TextView) findViewById(R.id.text_title);
        mTextCancel = (TextView) findViewById(R.id.tv_pass);
//        mTextReset = (TextView) findViewById(R.id.text_reset);
//        mTextReset.setClickable(false);
        mLockIndicator = (LockIndicator) findViewById(R.id.lock_indicator);
        mTextTip = (TextView) findViewById(R.id.text_tip);
        mGestureContainer = (FrameLayout) findViewById(R.id.gesture_container);
//        mSharedPreferences = this.getSharedPreferences("secret_protect", Context.MODE_PRIVATE);
        // 初始化一个显示各个点的viewGroup
        mGestureContentView = new GestureContentView(this, false, "", new GestureDrawline.GestureCallBack() {
            @Override
            public void onGestureCodeInput(String inputCode) {

                if (!isInputPassValidate(inputCode)) {
                    mTextTip.setText(Html.fromHtml("<font color='#c70c1e'>最少链接4个点</font>"));
                    mGestureContentView.clearDrawlineState(0L);

                    return;
                }

                if (mIsFirstInput) {
                    mFirstPassword = inputCode;

                    updateCodeList(inputCode);

                    mGestureContentView.clearDrawlineState(0L);
//                    mTextReset.setClickable(true);
//                    mTextReset.setText(getString(R.string.reset_gesture_code));
                } else {

                    if (inputCode.equals(mFirstPassword)) {
                        gesturePresent.gestureChange();

                    } else {
                        mTextTip.setText(Html.fromHtml("<font color='#c70c1e'>与上一次绘制不一致，请重新绘制</font>"));

                        // 左右移动动画
                        Animation shakeAnimation = AnimationUtils.loadAnimation(GestureEditActivity.this, R.anim.shake);
                        mTextTip.startAnimation(shakeAnimation);

                        // 保持绘制的线，1.5秒后清除
                        mGestureContentView.clearDrawlineState(1300L);
                    }
                }

                mIsFirstInput = false;
            }

            @Override
            public void checkedSuccess() {

            }

            @Override
            public void checkedFail() {

            }
        });


        // 设置手势解锁显示到哪个布局里面
        mGestureContentView.setParentView(mGestureContainer);

        updateCodeList("");
    }

    private void setUpListeners() {
        mTextCancel.setOnClickListener(this);
//        mTextReset.setOnClickListener(this);
    }

    private void updateCodeList(String inputCode) {

        mLockIndicator.setPath(inputCode);
        if(TextUtils.isEmpty(SpConfig.getInstance().getString(Constants.SHOU_SHI))){
            SpConfig.getInstance().edit().putString(Constants.SHOU_SHI,inputCode).commit();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_pass:
                if (SpConfig.getInstance().getBool(Constants.CHANGE_PWD)) {
                    openActivity(MainActivity.class);
                    SpConfig.getInstance().edit().putBoolean(Constants.CHANGE_PWD,false).commit();
                }
                finish();
                break;
//            case R.id.text_reset:
//                mIsFirstInput = true;
//                updateCodeList("");
//                mTextTip.setText(getString(R.string.set_gesture_pattern));
//                break;

            default:
                break;
        }
    }

    private boolean isInputPassValidate(String inputPassword) {

        if (TextUtils.isEmpty(inputPassword) || inputPassword.length() < 4) {
            return false;
        }

        return true;
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (SpConfig.getInstance().getBool(Constants.CHANGE_PWD)) {
                openActivity(MainActivity.class);
                SpConfig.getInstance().edit().putBoolean(Constants.CHANGE_PWD,false).commit();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public String gesturePassword() {
        return mFirstPassword;
    }

    @Override
    public String gestureCipher() {
        return null;
    }

    @Override
    public void onSuccess(Object object) {
        BaseBean baseBean = (BaseBean) object;
        if(baseBean.getError()==-1){
            Toast.makeText(GestureEditActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
            mGestureContentView.clearDrawlineState(0L);
            SpConfig.getInstance().edit().putString(Constants.SHOU_SHI, mFirstPassword).commit();
            SpConfig.getInstance().edit().putBoolean(Constants.IS_LOGIN, true).commit();
            if (SpConfig.getInstance().getBool(Constants.CHANGE_PWD)) {
                openActivity(MainActivity.class);
                SpConfig.getInstance().edit().putBoolean(Constants.CHANGE_PWD,false).commit();
            }
//                        openActivity(MainActivity.class);
//            if (SpConfig.getInstance().getBool(Constants.CHANGE_SHOUSHI)) {
////                finish();
//            } else {
////                            openActivity(MainActivity.class);
//                finish();
//            }
            finish();
        }else{
            toastShortShow(baseBean.getMsg());
        }
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 0;
    }
}
